<?php

!defined('IN_ROOT')	&& exit( 'Access Denied' );

class ProductController	extends	AdminModuleController
{
	/**
	 * 这个方法是临时的，大家用过就可以删了
	 * 提取并转换首字母的，并且把产品说明字段里面的html去除掉
	**/
	public function actionLetter() {
		$query = $this->db->query("SELECT productId,content FROM god_product");
		while($rs = $this->db->fetch_array($query)) {
			$content = strip_tags($rs['content']);
			$letterSort = $this->_letterSort($content);
			$this->db->update('god_product',array('letterSort'=>$letterSort,'content'=>$content),"productId='".$rs['productId']."'");
		}
		echo '转换完成';
	}
	
	public function	actionAdmin() {
		$page =	max(1, getgpc('page'));
		$categoryId = getgpc('categoryId');
		$keyword = getgpc('keyword');
		$sqladd = '';
		if(!empty($categoryId) && is_numeric($categoryId)){
			$sqladd	.= "categoryId='$categoryId' AND ";
		}
		if(!empty($keyword)){
			$sqladd .= "(productNo LIKE '%$keyword%' OR content LIKE '%$keyword%') AND ";
		}
		if(!empty($sqladd)){
			$sqladd = rtrim($sqladd,'AND ');
		}
		
		$num = $this->ProductModel->count($sqladd);
		$multipage = page($num,	GODHOUSE_PPP3, $page, "product-admin.htm?categoryId=$categoryId&keyword=$keyword");
		$products =	$this->ProductModel->findAll($sqladd, $page, GODHOUSE_PPP3);
		foreach($products as &$v) {
			$category =	$this->CategoryModel->findByPk($v['categoryId']);
			$v['categoryName'] = $category['cateName'];
			$v['createdTime2'] = date('Y-m-d', $v['createdTime']);
			$v['promotions'] = $v['ispromotions']==0?'':'<font color=red>【促销】</font>';
		}

		$optionListArr = array();
		$listArr = $this->CategorysPublic->get_list(GODHOUSE_CATEGORY_PRODUCT, 3);
		
		foreach($listArr as $key => $val) {
			$optionListArr[] = array('categoryId' => $val['categoryId'], 'cateName' => str_repeat('　', $val['level']) . '├ ' . $val['cateName']);
		}
		unset($listArr);

		$this->view->assign('optionListArr', $optionListArr);
		$this->view->assign('categoryId', $categoryId);	
		$this->view->assign('keyword', $keyword);
		$this->view->assign('multipage', $multipage);
		$this->view->assign('products',	$products);
		$this->display('product_admin');
	}

	public function	actionCreate() {
		if($_GET['action']=='Authentication' && !empty($_GET['productNo'])){
			if($this->ProductModel->count("productNo='{$_GET['productNo']}'")>0){
				exit('yes');
			} else {
				exit('no');
			}
		}
		
		if($product	= getgpc('product',	'P')) {
			if($this->ProductModel->count("productNo='{$product['productNo']}'")>0){
				$this->alert('订货号已经存在了!', 'product-create.htm');
			}
		
			if($product['categoryId'] > 0) {
				$parentid = $this->CategoryModel->oneColumn("categoryId = '".$product['categoryId']."' ", 'parentId');
				$i=3;
				while($parentid > 0){
					$i--;
					$p[$i] = $parentid;
					$parentid = $this->CategoryModel->oneColumn("categoryId = '$parentid' ", 'parentId');
				}
				$product['upperparentId'] = $p[1];
				$product['parentId'] = $p[2];
			}

			$upload	= new Upload();
			$upfile	= $upload->upfile($_FILES['picture'], 'upfile/product');

			if($string = $this->_letterSort($product['productNo'])) {
				$product['letterSort'] = $string;
			}
			$product['createdTime']	= $this->time;
			$product['picture']	= $upfile;
			$product['startDate'] =	strtotime($product['startDate']);
			$product['endDate']	= strtotime($product['endDate']);

			if($this->ProductModel->insert($product)) {
				$productCount =	$this->ProductModel->count("categoryId='{$product['categoryId']}'");
				$this->CategoryModel->update("categoryId='{$product['categoryId']}'",array('productCount' => $productCount));
			}
			$this->alert('添加成功。', 'product-admin.htm');
		}

		$this->view->assign('categorys', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_PRODUCT));
		$this->view->assign('brandArr', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_BRAND));
		$this->view->assign('action', 'create');
		$this->display('product_create');
	}

	public function	actionUpdate() {
		$productId = getgpc('productId');
		$updatetype = getgpc('updatetype');
		
		if($updatetype == 'ajaxupdate') {
			$content = getgpc('content', 'P');
			$this->ProductModel->updateByPk($productId,	array('content'=>$content));
			exit($content);
		}
		
		if($updatetype == 'ajaxsort') {
			$content = getgpc('productSort', 'P');
			
	
			$ss=$this->ProductModel->updateByPk($productId,	array('productSort'=>$content));
			
			
			exit($content);
		}
		
		$model = $this->ProductModel->findByPk($productId);
		if($product	= getgpc('product',	'P')) {
			if($_FILES['picture']['name']) {
				if($model['picture']) {
					@unlink(GODHOUSE_ROOT.substr($model['picture'],	1));
				}

				$upload	= new Upload();
				$upfile	= $upload->upfile($_FILES['picture'], 'upfile/product');
				$product['picture']	= $upfile;
			}
			if($string = $this->_letterSort($product['productNo'])) {
				$product['letterSort'] = $string;
			}
			$product['ispromotions'] = isset($product['ispromotions']) ? 1 : 0;
			$product['startDate'] =	strtotime($product['startDate']);
			$product['endDate']	= strtotime($product['endDate']);
			if($product['categoryId'] > 0) {
				$parentid = $this->CategoryModel->oneColumn("categoryId = '".$product['categoryId']."' ", 'parentId');
				$i=3;
				while($parentid > 0){
					$i--;
					$p[$i] = $parentid;
					$parentid = $this->CategoryModel->oneColumn("categoryId = '$parentid' ", 'parentId');
				}
				$product['upperparentId'] = $p[1];
				$product['parentId'] = $p[2];
			}

			$this->ProductModel->updateByPk($productId,	$product);
			if($product['categoryId']!=$model['categoryId'])
			{
				$productCount =	$this->ProductModel->count("categoryId='{$model['categoryId']}'");
				$this->CategoryModel->update("categoryId='{$model['categoryId']}'",array('productCount'	=> $productCount));

				$productCount =	$this->ProductModel->count("categoryId='{$product['categoryId']}'");
				$this->CategoryModel->update("categoryId='{$product['categoryId']}'",array('productCount' => $productCount));
			}

			$this->alert('修改成功。', 'product-admin.htm');
		}

		$model['startDate']	= $model['startDate']>0 ? date('Y-m-d',	$model['startDate']) :'';
		$model['endDate'] = $model['endDate']>0 ? date('Y-m-d', $model['endDate']) : '';

		$this->view->assign('categorys', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_PRODUCT));
		$this->view->assign('brandArr', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_BRAND));
		$this->view->assign('product', $model);
		$this->view->assign('productId', $productId);
		$this->view->assign('action','update');
		$this->display('product_create');
	}

	public function	actionDelete() {
		$productId = getgpc('productId');
		$model = $this->ProductModel->findByPk($productId);

		$this->ProductModel->deleteByPk($productId);
		$productCount =	$this->ProductModel->count("categoryId='{$model['categoryId']}'");
		$this->CategoryModel->update("categoryId='{$model['categoryId']}'",array('productCount'	=> $productCount));
		$this->redirect('product-admin.htm');
	}

	public function	actionAlldelete() {
		$productId = getgpc('productId','P');
		
		//$model = $this->ProductModel->findByPk($productId);
			
		$productId = implode(",",$productId);
			
		
			$rs=$this->ProductModel->delete("productId in (".$productId.") ");

			
		$productCount =	$this->ProductModel->count("categoryId='{$model['categoryId']}'");
		$this->CategoryModel->update("categoryId='{$model['categoryId']}'",array('productCount'	=> $productCount));
		$this->redirect('product-admin.htm');
	}
	
	private function _letterSort($string){
		$fchar=ord($string{0});
		if($fchar>=ord("A") and $fchar<=ord("z")) {
			return strtoupper($string{0});
		}
		if(is_numeric($string{0}) && $string{0}>=0 and $string{0}<=9) return $string{0};
		if($s=iconv("UTF-8","gb2312", $string)) {
			$asc=ord($s{0})*256+ord($s{1})-65536;
			if($asc>=-20319 and $asc<=-20284)return "A";
			if($asc>=-20283 and $asc<=-19776)return "B";
			if($asc>=-19775 and $asc<=-19219)return "C";
			if($asc>=-19218 and $asc<=-18711)return "D";
			if($asc>=-18710 and $asc<=-18527)return "E";
			if($asc>=-18526 and $asc<=-18240)return "F";
			if($asc>=-18239 and $asc<=-17923)return "G";
			if($asc>=-17922 and $asc<=-17418)return "I";
			if($asc>=-17417 and $asc<=-16475)return "J";
			if($asc>=-16474 and $asc<=-16213)return "K";
			if($asc>=-16212 and $asc<=-15641)return "L";
			if($asc>=-15640 and $asc<=-15166)return "M";
			if($asc>=-15165 and $asc<=-14923)return "N";
			if($asc>=-14922 and $asc<=-14915)return "O";
			if($asc>=-14914 and $asc<=-14631)return "P";
			if($asc>=-14630 and $asc<=-14150)return "Q";
			if($asc>=-14149 and $asc<=-14091)return "R";
			if($asc>=-14090 and $asc<=-13319)return "S";
			if($asc>=-13318 and $asc<=-12839)return "T";
			if($asc>=-12838 and $asc<=-12557)return "W";
			if($asc>=-12556 and $asc<=-11848)return "X";
			if($asc>=-11847 and $asc<=-11056)return "Y";
			if($asc>=-11055 and $asc<=-10247)return "Z";
			return null;
		} else {
			
			return false;
		}
	}
}
?>